﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPDemo
{
    public partial class RewardProduct_Details : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                int ID = Convert.ToInt32(Request.QueryString["productID"]);
                ViewState["productID"] = ID;
                dt = BackOffice_Maintenance.showRewardProduct(ID);
                
                prodImage.ImageUrl = dt.Rows[0][5].ToString();
                txtProductName.Text = dt.Rows[0][1].ToString();
                txtPoint.Text = dt.Rows[0][2].ToString();
                txtQuantity.Text = dt.Rows[0][3].ToString();
                rbStatus.SelectedValue = dt.Rows[0][4].ToString();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int result = 0;
            int point, quantity, Status;
            int count = 0;
            int id = Convert.ToInt32(ViewState["productID"]);
            string prodName = txtProductName.Text;

            DataTable dt = BackOffice_Maintenance.checkRewardNameUpdated(id, prodName);

            count = dt.Rows.Count;

            if (count == 0)
            {
                if (txtPoint.Text == "")
                    point = 0;

                else
                    point = Convert.ToInt32(txtPoint.Text);

                if (txtQuantity.Text == "")
                    quantity = 0;

                else
                    quantity = Convert.ToInt32(txtQuantity.Text);

                Status = Convert.ToInt32(rbStatus.SelectedValue);

                result = BackOffice_Maintenance.updateRewardProduct(id, prodName, point, quantity, Status);

                if (result > 0)
                {
                    //successfully update reward product
                }
            }

            else
            {
                int temp = Convert.ToInt32(dt.Rows[0][0]);

                if ((temp) == id)
                {
                    if (txtPoint.Text == "")
                        point = 0;

                    else
                        point = Convert.ToInt32(txtPoint.Text);

                    if (txtQuantity.Text == "")
                        quantity = 0;

                    else
                        quantity = Convert.ToInt32(txtQuantity.Text);

                    Status = Convert.ToInt32(rbStatus.SelectedValue);

                    result = BackOffice_Maintenance.updateRewardProduct(id, prodName, point, quantity, Status);

                    if (result > 0)
                    {
                        //successfully update reward product
                    }
                }

                else
                {
                    //cannot have product name
                }
            }
        }
    }
}